<?php

namespace App\Http\Controllers\Agent;

use App\Exceptions\BizException;
use App\Http\Controllers\Controller;
use App\Model\Agent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class AgentController extends Controller
{
    public function __construct()
    {
        $this->middleware(['agent'], ['except' => ['login', 'logout']]);
    }

    // 个人中心
    public function index()
    {
        return view('agent.agent.index', [
            'agent' => Auth::guard('agent')->user(),
        ]);
    }

    // 登录
    public function login(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $params = $request->all();
                if (empty($params['username']) || empty($params['password']))
                    throw new BizException('用户名或密码不能为空');

                $res = Auth::guard('agent')->attempt([
                    'username' => $params['username'],
                    'password' => $params['password'],
                ]);
                if (!$res)
                    throw new BizException('用户名或密码错误');

                $agent = Agent::where('username', $params['username'])->first();
                if ($agent['status'] != 1)
                    throw new BizException('您已被系统禁用');

            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
            return response()->json(['status' => 1, 'info' => '登录成功']);
        } else {
            return view('agent.agent.login');
        }
    }

    // 退出
    public function logout()
    {
        Auth::guard('agent')->logout();
        return redirect('agent/agent/login');
    }

    // 个人设置
    public function info(Request $request)
    {
        $agent = Auth::guard('agent')->user();
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'shop_name' => 'required',
                    'contact' => 'required',
                    'tel' => 'required',
                ], [
                    'shop_name.required' => '店铺名必填',
                    'contact.required' => '联系方人必填',
                    'tel.required' => '联系方式必填',
                ]);

                $params = $request->all();
                Agent::where('id', $agent->id)->update($params);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
            return response()->json(['status' => 1, 'info' => '修改成功']);
        } else {
            return view('agent.agent.info', [
                'row' => Agent::find($agent->id),
            ]);
        }
    }

    // 修改密码
    public function editPwd(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'old_password' => 'required',
                    'new_password' => 'required',
                    're_password' => 'required|same:new_password',
                ], [
                    'old_password.required' => '原密码必填',
                    'new_password.required' => '新密码必填',
                    're_password.required' => '确认密码必填',
                    're_password.same' => '新密码与确认密码不一致',
                ]);

                $agent = Auth::guard('agent')->user();
                $params = $request->all();

                $res = Auth::guard('agent')->attempt([
                    'username' => $agent->username,
                    'password' => $params['old_password'],
                ]);
                if (!$res)
                    throw new BizException('原密码不正确');

                Agent::where('id', $agent->id)->update(['password' => bcrypt($params['new_password'])]);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
            return response()->json(['status' => 1, 'info' => '修改成功']);
        } else {
            return view('agent.agent.edit-pwd');
        }
    }
}